//
//  SystemMessageeViewController.m
//  51易借
//
//  Created by 张睿 on 2017/8/21.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "SystemMessageeViewController.h"
#import "MessageTableViewCell.h"
#import "SystemDetailController.h"
#import "SystemListModel.h"
#import "MessageListModel.h"
@interface SystemMessageeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray * dataArr;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic,strong)ParaModel * para;
@property (nonatomic, strong)NSArray<SystemListModel *> * modelArray;

@end

@implementation SystemMessageeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getMessages];
    
    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{
    
    self.title = @"消息中心";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = ColorWithRGB(0xff7907);
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 64 * SCREENH6)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.bounces = YES;
    UIView *divisionView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView setTableFooterView:divisionView];
    [self.tableView registerClass:[MessageTableViewCell class] forCellReuseIdentifier:@"MessageCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.para.page = @(1);
        [self getMessages];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.para.page = @([self.para.page integerValue]+1);
        [self getMessages];

    }];
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];

    cell.model = self.dataArr[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * SCREENH6;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 删除数据源的数据,self.cellData是你自己的数据
//        [self.cellData removeObjectAtIndex:indexPath.row];
        // 删除列表中数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.dataArr removeObjectAtIndex:indexPath.row];
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";//默认文字为 Delete
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageListModel * model = self.dataArr[indexPath.row];;
    
    SystemDetailController * detailVC = [SystemDetailController new];
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.model = model;
    detailVC.titleString = model.name;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
}


- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)getMessages
{
    
    
    [[NetworkTool shareManager] POST:[Interfacefile loadMyMsgList] Param:[self.para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            if([self.para.page integerValue]==1)
            {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:[MessageListModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil]];
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
        
        
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } andFailure:^(NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self showHint:@"网络错误"];

    }];

}


-(ParaModel*)para
{
    if(!_para)
    {
        _para = [ParaModel new];
        _para.limit = @(10);
        _para.page = @(1);
    }
    return _para;
}



-(NSMutableArray*)dataArr
{
    if(!_dataArr)
    {
        _dataArr = [NSMutableArray array];
    
    }
    return _dataArr;
}

@end
